<?php

namespace app\admin\model\trades\assets;

use think\Db;
use think\Model;
use app\admin\model\vactivities\Activity;


class Order extends Model
{

    //支付类型
    const PAY_TYPE_1 = 1;//支付宝
    const PAY_TYPE_2 = 2;//微信

    //支付状态
    const PAY_STATE_1 = 1;//待支付
    const PAY_STATE_2 = 2;//已支付
    const PAY_STATE_3 = 3;//待退款
    const PAY_STATE_4 = 4;//已退款

    //支付状态
    const PayState = [
        self::PAY_STATE_1 => '待支付',
        self::PAY_STATE_2 => '已支付',
        self::PAY_STATE_3 => '待退款',
        self::PAY_STATE_4 => '已退款',
    ];

    //订单类型
    const ORDER_TYPE_1 = 1;//系列订单
    const ORDER_TYPE_2 = 2;//活动订单

    // 表名
    protected $table = 'activity_order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'paytime_text',
        'ctime_text',
        'utime_text',
        'paytype_text',
        'ordertype_text',
       // 'isend_text',
        'state_text',
    ];


    //绑定成功
    public function getStateList()
    {
        return ['1' => __('order_state 1'),'2' => __('order_state 2'),'3' => __('order_state 3'),'4' => __('order_state 4')];
    }

    //获取状态类型
    public function getStateTextAttr($value, $data)
    {
        $value = $value ? $value : $data['state'];
        $list = $this->getStateList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    //绑定成功
    public function getPaytypeList()
    {
        return ['2' => __('paytype 2'),'1' => __('paytype 1')];
    }
    //是否为默认绑定卡
    public function getOrdertypeList()
    {
        return ['0' => __('ordertype 0'),'1' => __('ordertype 1'),'2' => __('ordertype 2')];
    }
//    public function getIsendList()
//    {
//        return ['0' => __('isend 0'),'1' => __('isend 1')];
//    }

    //获取支付类型
    public function getPaytypeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['paytype'];
        $list = $this->getPaytypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    //获取活动类型
    public function getOrdertypeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['ordertype'];
        $list = $this->getOrdertypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

//    //获取活动结束标志
//    public function getIsendTextAttr($value, $data)
//    {
//        $value = $value ? $value : $data['isend'];
//        $list = $this->getIsendList();
//        return isset($list[$value]) ? $list[$value] : '';
//    }
    public function getPaytimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['paytime']) ? $data['paytime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getCtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['ctime']) ? $data['ctime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['utime']) ? $data['utime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setPaytimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setCtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function activity()
    {
        return $this->belongsTo('app\admin\model\vactivities\Activity', 'activity_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function serielactivity()
    {
        return $this->belongsTo('app\admin\model\vactivities\Seriesactivity', 'activity_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    /*
     *查找某用户是否设置了绑定提现账户
     */
    public function  getOrderValue($id){
        $where['id'] = $id;
        $where['state'] = self::PAY_STATE_2;
        $list = $this->getModel()->where($where)->find();
        if($list){
            return true;
        }else{
            return false;
        }

    }
    /*
     * 获取此用户是否过名
     */
    public function getUserIsEnroll($aid,$uid){

        if(empty($aid) || empty($uid))
            return false;

        $where['activity_id'] = $aid;
        $where['buyer_id'] = $uid;
        $where['ticket_type'] = Activity::TICKET_STATUS_0;
        $list = $this->getModel()->where($where)->find();
        if($list){
            return true;
        }else{
            return false;
        }
    }

    /*
     * 更新订单
     */
    public static function update_order($orderid,$sn){
        $param = [
            'paytime'=>time(),
            'state'=>self::PAY_STATE_2,
            'sn'=>$sn
        ];
        Db::table('activity_order')->where('orderid', $orderid)->update($param);//订单表更新
        return true;
    }
}
